package com.apex.lottery.common.result;

import lombok.Data;
import com.apex.lottery.common.errorcode.ErrorCode;
import com.apex.lottery.common.errorcode.GlobalErrorCodeConstants;

import java.io.Serializable;

/**
 * @ClassName CommonResult
 * @Description 统一返回格式
 * @Author ZJX
 * @Date 2024/9/22 13:19
 * @Version 1.0
 */

//    Serializable  序列化
@Data
public class CommonResult<T> implements Serializable {
    private Integer code;
    private String message;
    private T data;

    public static <T> CommonResult<T> success(T data){
        CommonResult<T> commonResult = new CommonResult();
        commonResult.code = GlobalErrorCodeConstants.SUCCESS.getCode();
        commonResult.data = data;
        commonResult.message = GlobalErrorCodeConstants.SUCCESS.getMessage();
        return commonResult;
    }

    public static <T> CommonResult<T> error(Integer code, String msg) {
        if (GlobalErrorCodeConstants.SUCCESS.getCode().equals(code)) {
            throw new AssertionError("code 不是错误的异常");
        }

        CommonResult<T> result = new CommonResult<>();
        result.code = code;
        result.message = msg;
        return result;
    }

    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMessage());
    }
}
